package com.qianying.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qianying.comment.CommonResult;
import com.qianying.entity.SysRole;
import com.qianying.service.SysRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统角色控制器
 * 处理角色管理相关的HTTP请求
 */
@RestController
@RequestMapping("/admin/sysRole")
@Tag(name = "系统角色管理", description = "系统角色的增删改查及状态管理接口")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 分页查询角色列表
     */
    @PostMapping("/getPage")
    @Operation(
            summary = "分页查询角色列表",
            description = "支持按角色名称、角色编码、状态等条件筛选，返回分页数据",
            parameters = {
                    @Parameter(name = "current", description = "页码，默认1", example = "1", required = false),
                    @Parameter(name = "size", description = "每页条数，默认10", example = "10", required = false),
                    @Parameter(name = "roleName", description = "角色名称(汉字)", example = "系统管理员", required = false),
                    @Parameter(name = "roleCode", description = "角色的英文code，如：ADMIN", example = "ADMIN", required = false),
                    @Parameter(name = "status", description = "是否禁用，0:启用,1:禁用", example = "0", required = false),
                    @Parameter(name = "sort", description = "角色顺序", example = "1", required = false)
            }
    )
    public CommonResult<IPage<SysRole>> getPage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestBody(required = false) SysRole sysRole) {
        Page<SysRole> page = new Page<>(current, size);
        return sysRoleService.getPage(page, sysRole == null ? new SysRole() : sysRole);
    }

    /**
     * 根据ID查询角色详情
     */
    @GetMapping("/getByRoleId")
    @Operation(
            summary = "查询角色详情",
            description = "通过主键ID获取角色的完整信息",
            parameters = {
                    @Parameter(name = "id", description = "角色主键ID", example = "1", required = true)
            }
    )
    public CommonResult<SysRole> getByRoleId(@RequestParam Long id) {
        return sysRoleService.getByRoleId(id);
    }

    /**
     * 新增角色
     */
    @PostMapping("/saveRole")
    @Operation(
            summary = "新增系统角色",
            description = "创建新的系统角色，角色名称和角色编码为必填项且唯一",
            parameters = {
                    @Parameter(name = "roleName", description = "角色名称(汉字)", example = "系统管理员", required = true),
                    @Parameter(name = "roleDesc", description = "角色描述", example = "拥有系统全部操作权限", required = true),
                    @Parameter(name = "roleCode", description = "角色的英文code，如：ADMIN", example = "ADMIN", required = true),
                    @Parameter(name = "sort", description = "角色顺序", example = "1", required = false),
                    @Parameter(name = "status", description = "是否禁用，0:启用,1:禁用，默认0", example = "0", required = false)
            }
    )
    public CommonResult<?> saveRole(@RequestBody SysRole sysRole) {
        return sysRoleService.saveRole(sysRole);
    }

    /**
     * 更新角色
     */
    @PostMapping("/updateRole")
    @Operation(
            summary = "更新系统角色",
            description = "修改已有系统角色信息，必须包含角色ID",
            parameters = {
                    @Parameter(name = "id", description = "角色主键ID", example = "1", required = true),
                    @Parameter(name = "roleName", description = "角色名称(汉字)", example = "系统管理员", required = true),
                    @Parameter(name = "roleDesc", description = "角色描述", example = "拥有系统全部操作权限", required = true),
                    @Parameter(name = "roleCode", description = "角色的英文code，如：ADMIN", example = "ADMIN", required = true),
                    @Parameter(name = "sort", description = "角色顺序", example = "1", required = false),
                    @Parameter(name = "status", description = "是否禁用，0:启用,1:禁用", example = "0", required = false)
            }
    )
    public CommonResult<?> updateRole(@RequestBody SysRole sysRole) {
        return sysRoleService.updateRole(sysRole);
    }

    /**
     * 更新角色状态
     */
    @PostMapping("/updateStatus")
    @Operation(
            summary = "更新角色状态",
            description = "单独更新角色的启用/禁用状态",
            parameters = {
                    @Parameter(name = "id", description = "角色主键ID", example = "1", required = true),
                    @Parameter(name = "status", description = "是否禁用，0:启用,1:禁用", example = "0", required = true)
            }
    )
    public CommonResult<?> updateStatus(
            @RequestParam Integer id,
            @RequestParam Integer status) {
        return sysRoleService.updateStatus(id, status);
    }

    /**
     * 根据ID删除角色
     */
    @PostMapping("/removeById")
    @Operation(
            summary = "删除系统角色",
            description = "通过主键ID删除指定的系统角色",
            parameters = {
                    @Parameter(name = "id", description = "角色主键ID", example = "1", required = true)
            }
    )
    public CommonResult<?> removeById(@RequestParam Integer id) {
        return sysRoleService.removeById(id);
    }

    /**
     * 查询所有角色
     */
    @GetMapping("/getAll")
    @Operation(
            summary = "查询所有系统角色",
            description = "返回所有系统角色列表，不分页，适用于下拉选择等场景"
    )
    public CommonResult<List<SysRole>> getAll() {
        return sysRoleService.getAll();
    }
}
